<style lang="less">
    .selectOption .ivu-select-dropdown-list{
        max-height: 300px;
        overflow: auto;
    }
</style>
<style lang="less" scoped>
@import "../../../assets/css/applyList.less";
</style>
<template>
    <div class="affiliationApplyThird search applyList">
        <Form ref="searchForm" :model="searchForm" inline :label-width="140" class="search-form rui-query-form">
            <div class="applyQuerySearchRow">
                <div class="applyQueryHead"></div>
                <div class="applyQuerySearchItem">
                    <Form-item label="" prop="socUniCrtCode">
                        <div>
                            <Input prefix="ios-search" type="text" v-model="searchForm.socUniCrtCode" @keyup.enter.native="handleSearch" clearable placeholder="统一社会信用代码"
                                   />
                            <a :style="{display:'block',width:'40px',lineHeight: '24px',marginLeft: '16px'}"  @click="dropDown">
                                {{dropDownContent}}
                                <Icon :type="dropDownIcon"></Icon>
                            </a>
                        </div>
                    </Form-item>
                </div>
            </div>
            <div v-if="drop" class="applyQuerySearchMore table-card">
                <div  class="applyQuerySearchMoreRow">
                    <div class="rui-query-form-item" style="display: none">
                        <Form-item label="挂靠类型" prop="busiType">
                            <Select v-model="searchForm.busiType" disabled placeholder="第三方公司" clearable>
                            </Select>
                        </Form-item>
                    </div>
                    <div class="rui-query-form-item">
                        <Form-item label="合作商名称" prop="channelName" >
                            <Select disabled class="selectOption" v-model="searchForm.channelName" filterable clearable >
                                <Option v-for="item in baseInfo" :value="item.channelFullName" :key="item.id">{{ item.channelFullName }}</Option>
                            </Select>
                        </Form-item>
                    </div>
                    <div class="rui-query-form-item">
                        <Form-item label="挂靠公司名称" prop="affiliatedName" >
                            <Input type="text" v-model="searchForm.affiliatedName" clearable placeholder="请输入" />
                        </Form-item>
                    </div>
                    <div class="rui-query-form-item">
                        <Form-item label="营业期限截止日期" >
                            <DatePicker v-bind:disabled="disabled" v-model="selectDate" type="daterange" format="yyyy-MM-dd" clearable
                                        @on-change="selectDateRange" placeholder="选择起始时间"
                                        style="    width:200px;"></DatePicker>
                            <Checkbox   v-model="searchForm.isOrNotLong" @on-change="selectCheck">长期</Checkbox>

                        </Form-item>
                    </div>
                    <div class="rui-query-form-item">
                        <Form-item label="状态" prop="status">
                            <Select v-model="searchForm.status" placeholder="请选择" clearable >
                                <Option v-for="item in statusList" :value="item.value" :key="item.value">{{ item.title }}
                                </Option>
                            </Select>
                        </Form-item>
                    </div>
                    <div class="rui-query-form-item">
                        <Form-item label="渠道归属" prop="channelBelong" >
                            <Select v-model="searchForm.channelBelong" @on-change="changeChannelBelong(searchForm.channelBelong)" placeholder="请选择" clearable >
                                <Option v-for="item in channelBelongList" :value="item.value" :key="item.value">{{ item.title }}
                                </Option>
                            </Select>
                        </Form-item>
                    </div>
                </div>
                <div style="text-align: center">
                    <Button @click="handleSearch" type="primary">查询</Button>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    <Button @click="handleReset">重置</Button>
                </div>
            </div>
        </Form>
        <div class="table-card">
            <div class="btn-status flex flex-between">
                <div class="loanApplyBtn">
                    <Button class="btn" size="default" @click="add">新增申请</Button>
                    <Button class="btn" type="error" size="default" @click="stop">批量停用</Button>
                    <circleLoading v-if="operationLoading"/>
                </div>
            </div>
            <div class="rui-table-box">
                <Table :loading="loading" :columns="columns" :data="data" sortable="custom"
                       @on-sort-change="changeSort" @on-selection-change="showSelect" ref="table">
                    <template slot-scope="{ row, index }" slot="action">
                        <div v-if="row.relStatus==='00'">
                            <Button style="margin-right:5px;" disabled  size="small">编辑
                            </Button>
                            <Button style="margin-right:5px;" disabled  size="small" >删除
                            </Button>
                        </div>
                        <div v-if="row.relStatus==='01'">
                            <Button style="margin-right:5px;" disabled  size="small">编辑
                            </Button>
                            <Button style="margin-right:5px;" disabled  size="small" >删除
                            </Button>
                        </div>
                        <div v-if="row.relStatus==='02' && row.isOrNotApply==='1'">
                            <Button style="margin-right:5px" type="primary" size="small" @click="edit(row)">编辑
                            </Button>
                            <Button style="margin-right:5px" type="error" size="small" @click="deleteById(row)">删除
                            </Button>
                        </div>
                        <div v-if="row.relStatus==='02' && row.isOrNotApply==='0'">
                            <Button style="margin-right:5px;" disabled  size="small">编辑
                            </Button>
                            <Button style="margin-right:5px;" disabled  size="small" >删除
                            </Button>
                        </div>
                        <div v-if="row.relStatus==='03' && row.isOrNotApply==='1'">
                            <Button style="margin-right:5px" type="primary" size="small" @click="edit(row)">编辑
                            </Button>
                            <Button style="margin-right:5px;" disabled  size="small" >删除
                            </Button>
                        </div>
                        <div v-if="row.relStatus==='03' && row.isOrNotApply==='0'">
                            <Button style="margin-right:5px" disabled size="small" >编辑
                            </Button>
                            <Button style="margin-right:5px;" disabled  size="small" >删除
                            </Button>
                        </div>
                        <div v-if="row.relStatus==='04'">
                            <Button style="margin-right:5px;" disabled  size="small">编辑
                            </Button>
                            <Button style="margin-right:5px;" disabled  size="small" >删除
                            </Button>
                        </div>
                        <div v-if="row.relStatus==='05'">
                            <Button style="margin-right:5px" type="primary" size="small" @click="edit(row)">编辑
                            </Button>
                            <Button style="margin-right:5px;" disabled size="small" >删除
                            </Button>
                        </div>
                    </template>

                    <template slot-scope="{ row, index }" slot="affiliatedName">
                        <a  type="primary" size="small" @click="show(row)">{{row.affiliatedName}}
                        </a>
                    </template>
                    <template slot-scope="{ row, index }" slot="channelNumber">
                        <a  type="primary" size="small" @click="showChannelName(row)">{{row.channelNumber}}
                        </a>
                    </template>
                    <template slot-scope="{ row, index }" slot="registeredAddress">
                        <span>{{getDetailAddress(row.registeredProvince,row.registeredCity,row.registeredAddress)}}</span>
                    </template>

                    <template slot-scope="{ row, index }" slot="status">
                        <Badge v-if="row.relStatus==='00'" text="启用" color="#67c23a"/>
                        <Badge v-else-if="row.relStatus==='01'" text="审核中" color="#999"/>
                        <Badge v-else-if="row.relStatus==='02'" text="草稿" color="#999"/>
                        <Badge v-else-if="row.relStatus==='03'" text="退回" color="#e6a23c"/>
                        <Badge v-else-if="row.relStatus==='04'" text="拒绝" color="#e6a23c"/>
                        <Badge v-else text="停用" color="#f56c6c"/>

<!--                        <div v-if="row.relStatus === '00'">-->
<!--                            <Tag color="success" size="default">启用</Tag>-->
<!--                        </div>-->
<!--                        <div v-else-if="row.relStatus === '01'">-->
<!--                            <Tag color="warning" size="default">审核中</Tag>-->
<!--                        </div>-->
<!--                        <div v-else-if="row.relStatus === '02'">-->
<!--                            <Tag color="default" size="default">草稿</Tag>-->
<!--                        </div>-->
<!--                        <div v-else-if="row.relStatus === '03'">-->
<!--                            <Tag color="warning" size="default">退回</Tag>-->
<!--                        </div>-->
<!--                        <div v-else-if="row.relStatus === '04'">-->
<!--                            <Tag color="error" size="default">拒绝</Tag>-->
<!--                        </div>-->
<!--                        <div v-else>-->
<!--                            <Tag color="error" size="default">停用</Tag>-->
<!--                        </div>-->
                    </template>
                    <template slot-scope="{ row, index }" slot="busiType">
                        <div v-if="row.busiType === '01'">
                            第三方公司
                        </div>

                    </template>
                </Table>
            </div>
            <br/>
            <Row type="flex" justify="end">
                <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize"
                      @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                      size="small" show-total show-elevator show-sizer></Page>
            </Row>
        </div>
        <Modal v-model="userModalVisible" :mask-closable='false' :width="400" :styles="{top: '180px'}">
            <Table :columns="columnsChannel" :data="dataChannel"></Table>
            <div slot="footer" style="display: none">

            </div>
        </Modal>
    </div>
</template>

<script>
    import {
        getAffiliationInfo,
        upDateStatusByIds,
        getChannelFullName,
        upDateCaseStatusByIds,
        deleteInfo,
    } from "@/projects/afs-apply/api/affiliation/affiliation";
    import { getLocation } from  "_p/afs-apply/api/apply-report/personal/common";
    import circleLoading from "_c/circle-loading/circle-loading.vue";
    import { getDictDataByType } from "@/projects/basic/api/admin/datadic";
    import { getChannelBaseInfo,getChannelId } from "@/projects/afs-apply/api/register-mng/listRecord";


    export default {
        name: "user-manage",
        components: {
            circleLoading
        },
        data() {
            return {
                loading: true,
                operationLoading: false,
                importLoading: false,
                loadingExport: true,
                disabled:null,
                userModalVisible: false,
                statusList:[],
                channelBelongList:[],
                baseInfo:[],
                channelId:'',
                selectBelong:'',
                drop: false,
                dropDownContent: "更多",
                dropDownIcon: "ios-arrow-down",
                viewImage: false,
                searchForm: {
                    busiType: "",
                    affiliatedName: "",
                    pageNumber: 1,
                    pageSize: 10,
                    socUniCrtCode: "",
                    status: "",
                    channelBelong:"",
                    channelName: "",
                    startDate: "",
                    endDate: "",
                    isOrNotLong:""
                },
                selectDate: null,
                modalType: 0,
                selectList:[],
                location:[],
                longTime:'',
                turn:'',
                selectCount:0,
                modalTitle: "",
                login:"SP",
                errorPass: "",
                province:[],
                city:[],
                submitLoading: false,
                columns: [
                    {
                        type: "selection",
                        minWidth: 60,
                        maxWidth:60,
                        align: "center",
                        fixed: "left"
                    },{
                        title:'序号',
                        type: 'index',
                        width: 80,
                        align: 'center',
                        ellipsis: true,
                        tooltip: true,
                        fixed: "left"
                    },
                    {
                        title: "挂靠公司名称",
                        key: "affiliatedName",
                        align: "center",
                        minWidth: 145,
                        slot:"affiliatedName",
                        fixed: "left"
                    },
                    {
                        title: "统一社会信用代码",
                        key: "socUniCrtCode",
                        align: "center",
                        minWidth: 140
                    },
                    {
                        title: "成立时间",
                        key: "foundedTime",
                        sortable: true,
                        align: "center",
                        minWidth: 150
                    },
                    {
                        title: "法人姓名",
                        key: "legalPersonName",
                        align: "center",
                        minWidth: 120
                    },
                    {
                        title: "注册地址",
                        key: "registeredAddress",
                        align: "center",
                        minWidth: 180,
                        slot:"registeredAddress"
                    },
                    {
                        title: "注册资本",
                        key: "registeredCapital",
                        align: "center",
                        minWidth: 120
                    },
                    {
                        title: "是否道路运输许可",
                        key: "whetherLicence",
                        align: "center",
                        minWidth: 120
                    },
                    {
                        title: "营业期限截止时间",
                        key: "oprTermValidity",
                        align: "center",
                        minWidth: 150
                    },
                    {
                        title: "状态",
                        key: "relStatus",
                        align: "center",
                        minWidth: 100,
                        slot: "status"
                    },
                    {
                        title: "操作",
                        key: "action",
                        minWidth: 150,
                        align: "center",
                        fixed: "right",
                        slot: "action"
                    }
                ],
                data: [],
                columnsChannel:[
                    {
                        title: "名称",
                        key: "channelFullName",
                        align: "center",
                        minWidth: 80,
                    },
                    {
                        title: "渠道归属",
                        key: "channelBelong",
                        align: "center",
                        minWidth: 60,
                    },
                ],
                dataChannel:[],
                total: 0
            };
        },
        watch: {
            '$route'(to,from){
                    if(from.path.indexOf("affiliationThirdEdit") || from.path.indexOf("affiliationThirdExamine")){
                        this.getAffiliationList(this.searchForm.pageNumber, this.searchForm.pageSize);
                }
            }
        },
        methods: {
            getProvinceData() {
                let param = {level:"1",upperCode: "1"};

                getLocation(param).then(res => {
                    if(res.code === "0000"){
                        this.province=res.data;
                    }
                });
            },
            getCityData() {
                let param = {level:2,upperCode:''};
                getLocation(param).then(res => {
                    if (res.code == "0000") {
                        this.city=res.data;
                    }
                });
            },
            //改变当前页码
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getAffiliationList(v, 10);
            },
            //改变页码数量
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getAffiliationList(1, v);
            },
            //选择时间期止
            selectDateRange(v) {
                if (v) {
                    this.longTime="0";
                    this.searchForm.startDate = v[0];
                    this.searchForm.endDate = v[1];
                }
            },
            //查询条件
              getAffiliationList(pageNumber = 1, pageSize = 10) {
                let params = {
                    pageNumber: pageNumber,
                    pageSize: pageSize,
                    busiType: "01",
                    startDate: this.searchForm.startDate,
                    endDate: this.searchForm.endDate,
                    affiliatedName: this.searchForm.affiliatedName,
                    socUniCrtCode: this.searchForm.socUniCrtCode,
                    status: this.searchForm.status,
                    channelBelong: this.searchForm.channelBelong,
                    channelId:this.channelId,
                    isOrNotLong:this.longTime,
                    isOrNotApply: "1"
                };
                if(this.searchForm.channelName!=""){
                    getAffiliationInfo(params).then(res => {
                        this.loading = false;
                        if (res.code === "0000") {
                            for (let i = 0; i < res.data.records.length; i++) {
                                let oprTermValidity = res.data.records[i].oprTermValidity;
                                let monthlyNum="";
                                if(typeof res.data.records[i].monthlySurplusNum!="undefined" && typeof res.data.records[i].monthlyApplicationNum !="undefined"){
                                    monthlyNum=res.data.records[i].monthlySurplusNum+"/"+res.data.records[i].monthlyApplicationNum;
                                }
                                res.data.records[i].monthlySurplusNum=monthlyNum;
                                let totalNum="";
                                if(typeof res.data.records[i].totalSurplusNum!="undefined" && typeof res.data.records[i].totalApplicationNum !="undefined"){
                                    totalNum=res.data.records[i].totalSurplusNum+"/"+res.data.records[i].totalApplicationNum;
                                }
                                res.data.records[i].totalSurplusNum=totalNum;
                                if (typeof oprTermValidity == "undefined" || res.data.records[i].longTermOrNot == "1") {
                                    res.data.records[i].oprTermValidity = "长期";
                                }
                                let whetherLicence=res.data.records[i].whetherLicence;
                                if(whetherLicence=="1"){
                                    res.data.records[i].whetherLicence="是";
                                }else if(whetherLicence=="0"){
                                    res.data.records[i].whetherLicence="否";
                                }
                            }
                            this.data = res.data.records;
                            this.total = res.data.total;
                        }
                    });
                }
            },
            //查询按钮操作
            handleSearch() {
                this.loading=true;
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getAffiliationList();
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            clearSelectAll() {
                this.$refs.table.selectAll(false);
            },

            // 停用
            stop() {
                if (this.selectCount <= 0) {
                    this.$Message.warning("您还未选择要停用的合作商");
                    return;
                }
                for (let i = 0; i < this.selectList.length; i++) {
                    if(this.selectList[i].relStatus!='00'){
                        this.$Message.warning("仅能停用已启用状态的合作商，请重新选择！");
                        this.clearSelectAll();
                        return;
                    }
                }
                this.$Modal.confirm({
                    title: "确认停用",
                    content: "您确认要停用所选的 " + this.selectCount + " 家合作商?",
                    onOk: () => {
                        let ids = "";
                        this.selectList.forEach(function (e) {
                            ids += e.relId + ",";
                        });
                        this.operationLoading = true;
                        upDateStatusByIds({ids:ids}).then(res => {
                            this.operationLoading = false;
                            if (res.code === "0000") {
                                this.$Message.success("状态更新成功");
                                this.getAffiliationList(this.searchForm.pageNumber,this.searchForm.pageSize);
                                upDateCaseStatusByIds({id:ids});
                            }
                        });
                    }
                });
            },
            //重置按钮操作
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.selectDate = null;
                this.searchForm.status="";
                this.searchForm.startDate = "";
                this.searchForm.endDate = "";
                this.searchForm.isOrNotLong=false;
                this.disabled=null;
                this.longTime="";
                // // 重新加载数据
                // this.loading=true;
                // this.getAffiliationList();
            },
            getStatus() {
                let param = {
                    type: "affiliationStatus"
                };
                getDictDataByType(param.type).then(res => {
                    if (res.code === "0000") {
                        this.statusList = res.data;
                    }
                });
            },
            getChannelBelong() {
                let param = {
                    type: "channelBelong"
                };
                getDictDataByType(param.type).then(res => {
                    if (res.code === "0000") {
                        this.channelBelongList = res.data;
                    }
                });
            },
            changeSort(e) {
                this.searchForm.sort = e.key;
                this.searchForm.order = e.order;
                if (e.order === "normal") {
                    this.searchForm.order = "";
                }
                this.getAffiliationList();
            },
            add() {
                this.afs.newTab(this,'projects/afs-apply/pages/affiliation-apply/affiliation-third/affiliationThirdAddEdit','第三方挂靠新增','ios-add',{type:1},'affiliation-third-add',[],true);
            },
            edit(v) {
                if (v.relStatus == "02" || v.relStatus == "05" || v.relStatus == "03") {
                    let param={
                        type:2,
                        id:v.id,
                        updateStatus:v.relStatus
                    }
                    this.turn="affiliationThirdEdit"+v.id;
                     this.afs.newTab(this,'projects/afs-apply/pages/affiliation-apply/affiliation-third/affiliationThirdAddEdit','第三方挂靠修改','ios-add',param,this.turn,[],true);
                } else {
                    this.$Message.error("只可修改草稿或停用状态的合作商!");
                }

            },
            show(v) {
                let param={
                    type:3,
                    id:v.id,
                }
                let turnPage="affiliationThirdShow"+v.id;
                this.afs.newTab(this,'projects/afs-apply/pages/affiliation-apply/affiliation-third/affiliationThirdAddEdit','第三方挂靠查看','ios-add',param,turnPage,[],true);
            },
            deleteById(row){
                this.$Modal.confirm({
                    title: "确认删除?",
                    onOk: () => {
                        let param={
                            id:row.id
                        }
                        deleteInfo(param).then(res => {
                            if (res.code === "0000") {
                                this.$Message.success("删除成功");
                                this.getAffiliationList(this.searchForm.pageNumber,this.searchForm.pageSize);
                            }
                        });
                    }
                });
            },
            showChannelName(v) {
                let id = v.id;
                this.operationLoading = true;
                getChannelFullName(id).then(res => {
                    if (res.code === "0000") {
                        let arr=[];
                        if(this.selectBelong==null || this.selectBelong==""){
                            arr=res.data;
                            for(let i=0;i<arr.length;i++){
                                if(arr[i].channelBelong=="00"){
                                    arr[i].channelBelong="SP";
                                }else if(arr[i].channelBelong=="01"){
                                    arr[i].channelBelong="直营";
                                }else if(arr[i].channelBelong=="02"){
                                    arr[i].channelBelong="总对总";
                                }
                            }
                        }else{
                            for(let i=0;i<res.data.length;i++){
                                if(res.data[i].channelBelong==this.selectBelong){
                                    arr.push(res.data[i]);
                                }
                            }
                            for(let i=0;i<arr.length;i++){
                                if(arr[i].channelBelong=="00"){
                                    arr[i].channelBelong="SP";
                                }else if(arr[i].channelBelong=="01"){
                                    arr[i].channelBelong="直营";
                                }else if(arr[i].channelBelong=="02"){
                                    arr[i].channelBelong="总对总";
                                }
                            }
                        }

                        this.dataChannel=arr;
                    }
                    this.operationLoading = false;
                });
                this.modalType = 0;
                this.userModalVisible = true;
            },
            dropDown() {
                if (this.drop) {
                    this.dropDownContent = "更多";
                    this.dropDownIcon = "ios-arrow-down";
                } else {
                    this.dropDownContent = "收起";
                    this.dropDownIcon = "ios-arrow-up";
                }
                this.drop = !this.drop;
            },
            selectCheck(value){
                if(value){
                    this.searchForm.selectDate="";
                    this.longTime="1";
                    this.searchForm.startDate="";
                    this.searchForm.endDate="";
                    this.disabled="disabled";
                }else{
                    this.longTime="";
                    this.disabled=null;
                    this.searchForm.selectDate="";
                    this.searchForm.startDate="";
                    this.searchForm.endDate="";
                }
            },
            getDetailAddress(registeredProvince,registeredCity,registeredAddress){
                if(registeredProvince!="" && registeredCity!="" && registeredAddress!=""){
                    let province="";
                    let city="";
                    for(let i=0;i<this.province.length;i++){
                        if(this.province[i].value==registeredProvince){
                            province=this.province[i].label;
                        }
                    };
                    for(let i=0;i<this.city.length;i++){
                        if(this.city[i].value==registeredCity){
                            city=this.city[i].label;
                        }
                    };
                    return province+city+registeredAddress;
                }else{
                    return '';
                }
            },
            changeChannelBelong(v){
                if(v!=null || v!=""){
                    this.selectBelong=v;
                }
            },
            getBaseInfo() {
                getChannelBaseInfo({status: ""}).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.baseInfo = res.data;
                        let baseInfoList=res.data;
                        getChannelId({}).then(ress => {
                            if (ress.code === "0000") {
                              // let channelId = "4701178224791771020";
                                  //let channelId = "4460600613838667776";
                                 let channelId = ress.data;
                        this.channelId=channelId;
                                if(typeof channelId!="undefined"){
                                    for(let i=0;i<baseInfoList.length;i++){
                                        if(channelId==baseInfoList[i].id){
                                            this.searchForm.channelName = baseInfoList[i].channelFullName;
                                        }
                                    }
                                }
                                this.getAffiliationList();
                            }
                        });
                    }
                });
            },
        },
        mounted() {
            this.getProvinceData();
            this.getCityData();
            this.getStatus();
            this.getChannelBelong();
            this.getBaseInfo();
        },

    };
</script>
